﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EyFramework.Common;
using YbService.Entity.Dto;

namespace HzybService.BaseOutData
{
    public class HzybOutData
    {
        /// <summary>
        /// 交易结果
        /// </summary>
        public int Result { set; get; }

        /// <summary>
        /// 交易返回字符串
        /// </summary>
        public string OutputStr { set; get; }

        /// <summary>
        /// 交易拆分后的中心交易流水号
        /// </summary>
        public string ZhongXinJylsh { set; get; }

        /// <summary>
        /// 交易拆分后的联机标记
        /// </summary>
        public string LianJiBz { set; get; }

        /// <summary>
        /// 交易拆分后的业务数据字符串
        /// </summary>
        public string OutDataStr { set; get; }

        public HzybOutData(HzybReturn hzybReturn)
        {
            Result = hzybReturn.YB_RESULT;
            OutputStr = hzybReturn.OUT_MESSAGE;

            if (Result >= 0)
            {
                string[] s = OutputStr.Split(new string[] { HzybConstants.p_大项分隔符 }, StringSplitOptions.None);
                int p = 0;
                ZhongXinJylsh = s[p++];
                LianJiBz = s[p++];
                OutDataStr = s[p++];
            }
        }

        protected string[] AnalyseRecordOutData()
        {
            string[] s = OutDataStr.Split(new string[] { HzybConstants.p_明细分隔符 }, StringSplitOptions.None);
            if (s.Length > 0)
            {
                if (s[s.Length - 1] == "")
                {
                    string[] tempS = new string[s.Length - 1];

                    for (int i = 0; i < s.Length - 1; i++)
                    {
                        tempS[i] = s[i];
                    }

                    s = tempS;
                }
            }
            return s;
        }

        protected int Get明细记录数()
        {
            return AnalyseRecordOutData().Length;
        }

        /// <summary>
        /// 获取单一记录的项
        /// </summary>
        /// <param name="Index">解析第几条记录,记录从0开始</param>
        /// <returns></returns>
        protected string[] AnalyseSingleOutData(int Index)
        {
            string[] s = AnalyseRecordOutData();
            if (Index + 1 > s.Length)
            {
                BizException ex = new BizException("解析返回值失败,当前返回的记录数小于你要取的记录下标");
                throw ex;
            }
            string[] ss = s[Index].Split(new string[] { HzybConstants.p_管道分隔符 }, StringSplitOptions.None);
            return ss;
        }

        /// <summary>
        /// 修改操作结果为成功
        /// </summary>
        /// <returns></returns>
        protected void ChangeResultToSucess()
        {
            Result = 0;
        }
    }
}